# Maintainer: David Grayson <davidegrayson@gmail.com>

_realname=libftdi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4
pkgrel=3
pkgdesc='Library to talk to FTDI chips, with Python 3 bindings (mingw-w64)'
arch=('any')
url="https://www.intra2net.com/en/developer/libftdi/"
license=('LGPL', 'GPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-python3")
depends=("${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-confuse"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
optdepends=("${MINGW_PACKAGE_PREFIX}-python3: Python3 bindings to libftdi")
options=('staticlibs' 'strip')
source=("https://www.intra2net.com/en/developer/libftdi/download/libftdi1-${pkgver}.tar.bz2"
        01-python-bindings.patch
        02-win32-issues.patch)
sha256sums=('ec36fb49080f834690c24008328a5ef42d3cf584ef4060f3a35aa4681cb31b74'
            '364428bf601f0574c5bcb9417c6ef629c669c53ea78f201c55e3e418fe9092ef'
            'f640cb6f0b4c86332941b1d5a5a9726c205b3f4078d4b4d5183c418e1e8cb97f')

prepare() {
  cd ${_realname}1-${pkgver}
  patch -p1 -i ${srcdir}/01-python-bindings.patch
  patch -p1 -i ${srcdir}/02-win32-issues.patch
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DEXAMPLES=OFF \
    -DPYTHON_BINDINGS=ON \
    -DDOCUMENTATION=OFF \
    "../${_realname}1-${pkgver}/"

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  PATH=$PATH:"${srcdir}/build-${MINGW_CHOST}/src" make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/${_realname}1-${pkgver}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  cp COPYING* "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  sed -i "s;$(cygpath -m /);/;" \
      "${pkgdir}${MINGW_PREFIX}/lib/cmake/${_realname}1"/*.cmake
}
